package crawlerGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import scheduler.CrawlerScheduler;
import scheduler.ILinkRepository;
import scheduler.MySQLLinkRepository;
import server.CrawlerServer;
import structures.InfoPacket;
import structures.TaskType;
import crawler.CrawlerClient;

public class CrawlerMainWindow extends JFrame {

	private static final long serialVersionUID = 1L;

	// panel z zakłądkami
	private JTabbedPane tabbedPane;
	// zakłądka SearchWord
	private JPanel panel1;
	// guzik SEARCH
	private JButton button1;
	// guzik Search links
	private JButton button2;
	// druga zakłądka
	private JPanel panel2;
	// trzecia zakłądka
	private JPanel panel3;
	// pole w pierwszej zakłądce
	private JTextArea textArea1;
	// pole w drugiej zakładce
	private JTextArea textArea2;

	// serwer crawlera
	private CrawlerServer srv;
	private ILinkRepository rep;
	// private JPanel clientPanel;
	// pole w pierwszej zakładce
	private JTextField textField11;
	// pole po prawej od zakłądek z aktualnymi klientami
	private JTextArea textAreaMainRight;
	LinkedList<String> linkList = new LinkedList<String>();
	// textfield na linki
	JTextField textFieldAddLinks;
	// textArea na linki
	JTextArea textAreaAddLinks;

	// startuje serwer lub ustawia nowe zadanie
	public void startServer(InfoPacket pck) {

		linkList.add("http://simple.wikipedia.org/wiki/Main_Page");
		// ILinkRepository rep = new LinkRepository();
		rep = new MySQLLinkRepository("jdbc:mysql://localhost/netcrawler",
				"root", "password");
		rep.init(linkList);
		// czyszczenie po dodaniu linków
		if (srv == null) { // sprawdzamy czy już jest server odpowiadajacy temu
							// okienku, ew. jeszcze pobieramy port
			srv = new CrawlerServer(5775, new CrawlerScheduler(rep), rep, this,
					pck);
			new CrawlerClient(5775, "127.0.0.1", 2).start();
			Thread t = new Thread(srv);
			t.start();
		} else
			srv.updatePacket(pck);

	}

	// main ----
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CrawlerMainWindow window = new CrawlerMainWindow();
				window.createGUI();
			}
		});
	}

	// tworzenie głównego okna
	public void createGUI() {

		this.setName("Crawler Command Center");
		this.setSize(900, 500);

		Container content = this.getContentPane();
		content.setBackground(Color.LIGHT_GRAY);

		// tworzenie tabów
		CreateStringSearchWindow();
		CreateURLSearchWindow();
		CreateAddLinksWindow();

		// panel główny
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
		content.add(topPanel);

		// panel po lewej
		this.tabbedPane = new JTabbedPane();
		this.tabbedPane.addTab("WordSearch", panel1);
		this.tabbedPane.addTab("URLSearch", panel2);
		this.tabbedPane.addTab("AddLinks", panel3);

		// panel po prawej
		textAreaMainRight = new JTextArea();
		textAreaMainRight.setLineWrap(true);
		textAreaMainRight.setWrapStyleWord(true);
		textAreaMainRight.setBorder(BorderFactory.createLineBorder(
				Color.LIGHT_GRAY, 10));
		textAreaMainRight.setEditable(false);

		JPanel eastPanel = new JPanel();
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
		JScrollPane pane = new JScrollPane();
		pane.getViewport().add(textAreaMainRight);
		eastPanel.add(pane);

		// dodanie obu głównych paneli
		topPanel.add(tabbedPane);
		topPanel.add(eastPanel);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	// tworzenie zakładki szukającej słów kluczowych
	public void CreateStringSearchWindow() {
		// szukanie stringow
		panel1 = new JPanel();
		panel1.setLayout(new BorderLayout());
		panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		textArea1 = new JTextArea();
		JScrollPane pane = new JScrollPane();

		textArea1.setLineWrap(true);
		textArea1.setWrapStyleWord(true);
		textArea1.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		textArea1.setEditable(false);

		pane.getViewport().add(textArea1);
		panel1.add(pane, BorderLayout.CENTER);

		textField11 = new JTextField();
		textField11.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
		textField11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				startWordSearch();

			}
		});
		JPanel tempPanel = new JPanel();
		tempPanel.setLayout(new BorderLayout());

		button1 = new JButton("SEARCH");
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				startWordSearch();

			}
		});

		JButton button3 = new JButton("STOP");
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						srv.stop();
					}
				}).start();
				// srv = null;

			}
		});

		tempPanel.add(button1, BorderLayout.EAST);
		tempPanel.add(button3, BorderLayout.WEST);
		tempPanel.add(textField11, BorderLayout.CENTER);
		JLabel label = new JLabel("Strony zawierające podane słowo kluczowe:\n");
		panel1.add(label, BorderLayout.NORTH);
		panel1.add(tempPanel, BorderLayout.SOUTH);
	}

	// tworzenie zakładki szukającej słów kluczowych
	public void CreateAddLinksWindow() {
		// szukanie stringow
		panel3 = new JPanel();
		panel3.setLayout(new BorderLayout());
		panel3.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		textAreaAddLinks = new JTextArea();
		JScrollPane pane = new JScrollPane();

		textAreaAddLinks.setLineWrap(true);
		textAreaAddLinks.setWrapStyleWord(true);
		textAreaAddLinks.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		textAreaAddLinks.setEditable(false);

		pane.getViewport().add(textAreaAddLinks);
		panel3.add(pane, BorderLayout.CENTER);

		textFieldAddLinks = new JTextField();
		textFieldAddLinks.setBorder(BorderFactory.createLineBorder(Color.GRAY,
				1));
		textFieldAddLinks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				String tmp = textFieldAddLinks.getText();
				linkList.add(tmp);
				textAreaAddLinks.append(tmp);
				if (rep != null) {
					rep.saveLinks(linkList);
				}
			}
		});
		JPanel tempPanel = new JPanel();
		tempPanel.setLayout(new BorderLayout());

		JButton button = new JButton("Add");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {

				String tmp = textFieldAddLinks.getText();
				linkList.add(tmp);
				textAreaAddLinks.append(tmp + "\n");
				if (rep != null) {
					rep.saveLinks(linkList);
				}

			}
		});
		tempPanel.add(button, BorderLayout.EAST);
		tempPanel.add(textFieldAddLinks, BorderLayout.CENTER);
		JLabel label = new JLabel("Dodane linki:\n");
		panel3.add(label, BorderLayout.NORTH);
		panel3.add(tempPanel, BorderLayout.SOUTH);

	}

	public void startWordSearch() {
		textArea1.setText("");
		textArea2.setText("");
		InfoPacket pck = new InfoPacket();
		pck.addTask(TaskType.keyword, textField11.getText());
		startServer(pck);
	}

	// tworzenie zakładki z linkami
	public void CreateURLSearchWindow() {
		// szukanie stringow
		panel2 = new JPanel();
		panel2.setLayout(new BorderLayout());
		panel2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		textArea2 = new JTextArea();
		JScrollPane pane = new JScrollPane();

		textArea2.setLineWrap(true);
		textArea2.setWrapStyleWord(true);
		textArea2.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		textArea2.setEditable(false);
		pane.getViewport().add(textArea2);
		panel2.add(pane);

		button2 = new JButton("Search");
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				textArea1.setText("");
				textArea2.setText("");
				startServer(new InfoPacket());
			}
		});
		JLabel label = new JLabel("Wyszukane linki:\n");
		panel2.add(label, BorderLayout.NORTH);
		panel2.add(button2, BorderLayout.SOUTH);
	}

	// dodawanie nowych linków
	public void updateURLs(List<String> URLs) {
		for (String s : URLs) {
			// oczyszcza i wsadza nowe linki
			textArea2.setText("");
			textArea2.append(s + "\n");
		}
	}

	// dodawanie stron z szukanym słowem
	public void updatePhrasesFound(String found) {
		textArea1.append(found + "\n");
	}

	// dodawanie nowych klientów
	public void addClient(String name) {
		this.textAreaMainRight.append("Client address = " + name + "\n");
	}

}